package com.video.main_video.manager.handle.userInfo;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.video.main_video.api.Api;
import com.video.main_video.common.jwt.JwtUtils;
import com.video.main_video.common.utils.ImgUtils;
import com.video.main_video.dao.entity.User;
import com.video.main_video.dao.entity.VideoInfo;
import com.video.main_video.dao.request.userInfo.UserVideoRequest;
import com.video.main_video.dao.response.userInfo.UserVideoResponse;
import com.video.main_video.dao.response.userInfo.VideoModel;
import com.video.main_video.manager.handle.ApiHandler;
import com.video.main_video.manager.mapper.VideoInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api
public class UserVideoManager implements ApiHandler<UserVideoRequest, UserVideoResponse> {

    private long totalPage=0;
    @Autowired
    VideoInfoMapper videoInfoMapper;
    @Override
    public UserVideoResponse handler(UserVideoRequest userVideoRequest) {
        UserVideoResponse res=new UserVideoResponse();
        boolean checkResult = checkUserId(userVideoRequest.getUserId(), userVideoRequest.getAccessToken());
        if (!checkResult){
            res.setCode(404);
            res.setError("验证失败");
            return res;
        }

        List<VideoModel> videos=getUserVideo(userVideoRequest);

        res.setVideoModel(videos);
        res.setTotalPage(totalPage);
        res.setCode(200);
        return res;
    }

    private List<VideoModel> getUserVideo(UserVideoRequest req){

        Page<VideoInfo> page=new Page<>(req.getCurrentPage(),req.getPageSize());

        LambdaQueryWrapper<VideoInfo> qw=new LambdaQueryWrapper<>();
        qw.eq(VideoInfo::getUserId,req.getUserId());

        Page<VideoInfo> result = videoInfoMapper.selectPage(page, qw);
        List<VideoInfo> list=result.getRecords();
        List<VideoModel> model=new ArrayList<>();
        for (VideoInfo v:list) {
            VideoModel info=new VideoModel();
            String base64png="";
            try{
                base64png=ImgUtils.compressImageByFile(new File(v.getVideoImage()));
            }catch (Exception e){
                e.printStackTrace();
                continue;
            }
            BeanUtils.copyProperties(v,info);
            info.setVideoCover("data:image/png;base64,"+base64png);
            model.add(info);
       }
        this.totalPage=result.getTotal();
        return model;
    }

    private boolean checkUserId(String userId,String token){
        System.out.println(token);
        DecodedJWT verify = JwtUtils.verify(token);
        String id = null;
        if (verify != null) {
            id = verify.getClaim("userId").asString();
        }
        return Objects.equals(userId, id);
    }
}
